#!/bin/bash
# Create & manage k8s clusters

function usage(){
  echo -e "\033[33mUsage:\033[0m ./ica <step>"
  cat <<EOF

available steps:
    01  nginx              to install nginx external loadbalance
    02  keepalived         to install keepalived high availability for nginx(1 master and 1 backup)
    03  mysql              to install mysql(1 master and n slave)
    04  jdk                to install jdk
    05  maven              to install maven
    06  docker             to install docker and docker-compose
    07  zookeeper          to install zookeeper cluster
    08  kafka              to install kafka cluster
    09  kafka-kraft        to install kafka kraft mode cluster
    10  prometheus         to install prometheus server
    11  node_exporter      to install node_exporter
    12  alertmanager       to install alertmanager
    13  elasticsearch      to install elasticsearch
    14  kibana             to install kibana
    15  filebeat           to install filebeat
    16  logstash           to install logstash
    17  harbor             to install harbor
    18  gitlab-runner      to install gitlab-runner
    90  all                to run 01~11 all at once 

examples: ./ica 01  (or ./ica nginx)
          ./ica 03  (or ./ica mysql)
          ./ica all (or ./ica 90)

EOF
}


function setup() {

    case "$1" in
      (01|nginx)
          PLAY_BOOK="01.nginx.yaml"
          ;;
      (02|keepalived)
          PLAY_BOOK="02.keepalived.yaml"
          ;;
      (03|mysql)
          PLAY_BOOK="03.mysql.yaml"
          ;;
      (04|jdk)
          PLAY_BOOK="04.jdk.yaml"
          ;;
      (05|maven)
          PLAY_BOOK="05.maven.yaml"
          ;;
      (06|docker)
          PLAY_BOOK="06.docker.yaml"
          ;;
      (07|zookeeper)
          PLAY_BOOK="07.zookeeper.yaml"
          ;;
      (08|kafka)
          PLAY_BOOK="08.kafka.yaml"
          ;;
      (09|kafka-kraft)
          PLAY_BOOK="09.kafka-kraft.yaml"
          ;;
      (10|prometheus)
          PLAY_BOOK="10.prometheus.yaml"
          ;;
      (11|node_exporter)
          PLAY_BOOK="11.node_exporter.yaml"
          ;;
      (12|alertmanager)
          PLAY_BOOK="12.alertmanager.yaml"
          ;;
      (13|elasticsearch)
          PLAY_BOOK="13.elasticsearch.yaml"
          ;;
      (14|kibana)
          PLAY_BOOK="14.kibana.yaml"
          ;;
      (15|filebeat)
          PLAY_BOOK="15.filebeat.yaml"
          ;;
      (16|logstash)
          PLAY_BOOK="16.logstash.yaml"
          ;;
      (17|harbor)
          PLAY_BOOK="17.harbor.yaml"
          ;;
      (18|gitlab-runner)
          PLAY_BOOK="18.gitlab-runner.yaml"
          ;;
      (90|all)
          PLAY_BOOK="90.all.yml"
          ;;
      (*)
    	      usage
          exit 1 
          ;;
    esac

    COMMAND="ansible-playbook -i hosts playbooks/$PLAY_BOOK"
    echo "$COMMAND"
    ${COMMAND} || return 1
}

function main() {

[ "$#" -eq 1 ] || { usage >&2; exit 2; }
setup $1

}
main "$@"
